<?php 
class MyqiugouAction extends Action{
	
    public function _initialize() {
	}
	
	public function index() {
		
		$goods = M( 'qiugou' );
		$count=$goods->where('uid='.$this->mid)->count();
		$p = new Page ( $count, 15 );
		$goodslist=$goods->where('uid='.$this->mid)->limit($p->firstRow.','.$p->listRows)->order('utime DESC')->findAll();
		$page = $p->show ();
		$this->assign ( "page", $page );
		//print_r($goodslist);
		$this->assign('goodslist',$goodslist);
	    $this->setTitle('我是买家');
		$this->display();
	}
	
	public function add(){
		$act = add;
		$type = M('goods_type');
		$goodstype=$type->order('display_id')->findAll();
		$userinfo = M('user_basicfile')->field('province, city, school')->where('uid='.$this->mid)->find();
		//print_r($userinfo);
		$this->assign('act',$act);
		$this->assign('userinfo',$userinfo);
		$this->assign('goodstype',$goodstype);
		$this->setTitle('发布求购信息');
		$this->display('edit');
		
	}
	public function edit(){
		$gid  =  empty($_GET['id'])? '' : intval($_GET['id']);
		if(empty($gid)){
		     $this->error('您查看的页面不存在，或者已经删除');	
		}
		$map['id']=$gid;
		$map['uid']=$this->mid;
		$goods = M('qiugou')->where($map)->find();
		if($goods){
		$this->assign('goods',$goods);	
		$goodstype=M ( 'goods_type' )->order('display_id')->findAll();
		$this->assign('goodstype',$goodstype);
		$this->setTitle('编辑求购信息');
		$this->display();
		}else{
			
			$this->error('您查看的页面不存在，或者已经删除');
		}
	}
	public function doDel(){
		$id  = empty($_POST['id'])? '' : intval($_POST['id']);
		if(empty($id)){
		 echo 0;
         return;	 
		}
	        $map['id'] =$id;
			$map['uid']=$this->mid;
			
			$rel=M('qiugou')->where($map)->delete();
            $map2['shop_id'] =$this->mid;
			$map2['goods_id']=$id;
			$map2['type']    ='qiugou';
			M('shop_leaveword')->where($map2)->delete();
			echo ($res = $rel)? 1:0;
	}
    public function dochekall(){
    	if($_POST['title']==''){
    		
    	   $this->error('标题不能为空');
    	}elseif($_POST['name']==''){
    		$this->error('商品名称不能为空');
    	}elseif($_POST['g_details']==''){
    	   $this->error('详细内容不能为空');		
    	}elseif($_POST['area_province']==0 || $_POST['area_city']==0){
		   $this->error('请选择城市');
		}else{
    	$this->saveform();	
    		
    	}
    	
    }
	protected function saveform(){
		if($_POST['id']){
		  $qiugou = M('qiugou');
		  $data['id']          =  intval($_POST['id']);
		  $data['title']       =  t($_POST['title']);
		  $data['gname']       =  t($_POST['name']);
		  $data['gprice']      =  t($_POST['price']);
		  $data['gnumber']     =  t($_POST['number']);
		  $data['gtype']       =  intval($_POST['type']);
		  $data['province']    =  intval($_POST['area_province']);
		  $data['city']        =  intval($_POST['area_city']);
		  $data['location']    =   getLocation($data['province'],$data['city']);
		  $data['school']      =  $_POST['school'];
		  $data['gdetails']    =  h(keyWordFilter($_POST['g_details']));
		  $data['utime']       =  time();
		
		 if($qiugou->save($data)) {
    			$this->success('数据更新成功！');
          }else{
            	//echo $goods->getLastSql(); 
                $this->error('数据更新失败');
          }
		}else{
		    $qiugou = M('qiugou');
		    $data['uid']         =   $this->mid;
			$data['uname']       =   $this->user['uname'];
		    $data['title']       =   t($_POST['title']);
		    $data['gname']       =   t($_POST['name']);
		    $data['gprice']      =   t($_POST['price']);
		    $data['gnumber']     =   t($_POST['number']);
		    $data['gtype']       =   intval($_POST['type']);
		    $data['province']    =   intval($_POST['area_province']);
		    $data['city']        =   intval($_POST['area_city']);
		    $data['location']    =   getLocation($data['province'],$data['city']);
		    $data['school']      =   $_POST['school'];
		    $data['gdetails']    =   h(keyWordFilter($_POST['g_details']));
		    $data['utime']       =  $data['ctime'] =   time();
		        
		    if(false !==$qiugou->add($data)) {
    	        $this->success('数据添加成功！');
            }else{
             	//echo $goods->getLastSql(); 
                $this->error('数据写入错误');
                //Exit;
               
            }
		}
	}
	
}



?>